//
//  NetworkingHandler.m
//  UI29_Block
//
//  Created by dllo on 16/1/21.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "NetworkingHandler.h"

@implementation NetworkingHandler
+ (void)networkingWithUrl:(NSString *)string completeHandle:(void(^)(id result))block {
    
    // 字符串转码
    NSString *codeStr =[string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:codeStr];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        // 回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            
            // 解析数据
            if (data) {
                id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                
                // 调用block
                block(result);
            }
            
            
            
        });
        
    }];
    
    [task resume];
    
}
@end
